home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 2804 < prev    next >
Encoding:
Text File  |  1996-08-06  |  3.3 KB  |  89 lines

  1. Path: atglab.bls.com!Alun.Champion
  2. From: Alun.Champion@bridge.bst.bls.com (Alun Champion)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: [Q] Dynamically allocating memory for a char*
  5. Date: 19 Jan 1996 20:50:29 GMT
  6. Organization: Computer People Inc.
  7. Message-ID: <ALUN.CHAMPION.96Jan19155029@g7240065.bridge.bst.bls.com>
  8. References: <4dmn1i$10t@walrus2.walrus.com>
  9.     <NITIN.96Jan19095012@more.eng.sun.com>
  10. NNTP-Posting-Host: bstfirewall.bst.bls.com
  11. In-reply-to: nitin@more.eng.sun.com's message of 19 Jan 1996 17:50:12 GMT
  12.  
  13. In article <NITIN.96Jan19095012@more.eng.sun.com> nitin@more.eng.sun.com (Nitin More [CONTRACTOR]) writes:
  14.  
  15. : [original attribution not included in response]
  16. : > I have a question about allocating memory for a string as it's passed
  17. : > in by the user.  I'm using cin to get the info, but maybe this is
  18. : > inappropriate.  I have included the relevant snippets of the file
  19. : > below.  What I am trying to achieve is no limitation for the user when
  20. : > they input a path + filename.  Is it possible to use char* foo and
  21. : > then something like (I know this is wrong, but the idea of it ...)
  22.  
  23. : To read a string from cin, you need a place to store it (char *) with enough
  24. : memory allocated.  Until you read the string, you won't know what is enough.
  25. : This is a catch-22.  The best you can do is use a temporary variable with
  26. : (reasonable) maximum possible size, read the string in the temporary variable,
  27. : get the size plus one for the NULL character, allocate that much memory
  28. : pointed to by your original variable and copy the string into it from the
  29. : temporary variable.
  30.  
  31. : Since you may get into situation where the input string is bigger than the
  32. : maximum size you have chosen, you could do the following.  Set the last char
  33. : in the temporary variable to be NULL (i.e. 0) and check if it is still the
  34. : same after reading the string.  If it is same then you are safe.  If not, the
  35. : string read is bigger than your maximum size and you don't have the complete
  36. : string.  [Worse yet, you might have overwritten memory of some other
  37. : variable].  Either you can display an error and quit or reallocate bigger size
  38. : (e.g., 2*maximum) for the temporary and try again.  Try until you succeed.
  39.  
  40. : Anybody else knows a better way?
  41.  
  42. Yes.
  43. You could use a string class, which already deals with dynamic allocation of
  44. strings. As most have stream operators defined on them, they work exactly
  45. as desired.
  46.  
  47. or you could write the class yourself
  48.  
  49. or use a strstream:
  50.  
  51.    char ch;
  52.    ostrstream ostr;
  53.  
  54.    // If strings are defined as white space delimited then you probably
  55.    // want to gobble up any leading white space before entering the
  56.    // reading loop. Maybe you should check cin.flags() & ios::skipws != 0
  57.    while (cin >> ch) {
  58.        if (isspace(ch)) {
  59.            cin.putback(ch);
  60.            break;
  61.        }
  62.        ostr << ch;
  63.    }
  64.  
  65.    ostr << ends;
  66.    char* filename = ostr.str();
  67.  
  68.    ... do stuff ...
  69.  
  70.    delete[] filename;       // Don't forget to clean up your responsibilites.
  71.  
  72. or if you can guarentee the filename will end at eof ('; which is rarely
  73. the case ;') then more simply:
  74.  
  75.    ostrstream ostr;
  76.    cin >> ostr.rdbuf();
  77.    ostr << ends;
  78.    char* filename = ostr.str();
  79.  
  80.    ... do stuff ...
  81.  
  82.    delete[] filename;       // Don't forget to clean up your responsibilites.
  83.  
  84. Regards
  85.  
  86.    -A.
  87. -- 
  88. | A.Champion                |
  89.